package GDoc::Job::Fetch::US::Exec::En::EIA::PMM;

use Moose;

use GDoc::Job;
use File::Temp;

extends 'GDoc::Job::Fetch';

my @MONTHS = qw{January February March April May June July August September October November December};

sub setup { 
    my $self = shift;
 
    # NOTE: No basedir because PDFs are relative to each report's own enclosing directory
    $self->_baseurl( 'http://www.eia.doe.gov/oil_gas/petroleum/data_publications/petroleum_marketing_monthly/pmm_historical.html' );
}

sub run { 
    my $self = shift;

    if ( $self->data->{link} ) { 
        $self->_fetch_pdf;
    } else { 
        $self->logger->info( "fetching base " . $self->_baseurl );

        my $mech = $self->_mech;
        $mech->get( $self->_baseurl );

        my @idx_links = $mech->find_all_links( url_regex => qr{^pmmh_\d{4}_\d{2}\.html$} );
        foreach my $idx_link( @idx_links ) { 
            $self->logger->info( "getting index link" . $idx_link->url );

            $mech->get( $idx_link );
            my @links = $mech->find_all_links( url_regex => qr{/pmm_\d{4}_\d{2}\.html$} );

            foreach my $link( @links ) { 
                my $job = GDoc::Job->new( class => 'Fetch::US::Exec::En::EIA::PMM',
                                          queue => $self->fetch_queue,
                                          data  => { link  => $link->url }
                                        );
                $job->submit;
            }

            $mech->back;
        }
    }
}


sub _fetch_pdf { 
    my $self = shift;
    my %data = %{ $self->data };

    $self->logger->info( "getting report page $data{link}" );
    my $mech = $self->_mech;
    $mech->get( $data{link} );

    my $pdf_link = $mech->find_link( url_regex => qr{/pmmall\.pdf} );
    my $pdf_url = $pdf_link->url;

    unless( $pdf_link ) { 
        $self->logger->info( "No PDF link found at $data{link}, skipping..." );
        return;
    }

    $data{link} =~ m{/pmm_(?<year>\d{4})_(?<month>\d{2})\.html$};
    my ( $year, $month ) = @+{ 'year', 'month' };

    my $ident = sprintf( 'EIA-PMM-%04d-%02d', $year, $month );
    my $filename = $ident . '.pdf';
    my $key = 'us/exec/en/eia/' . $filename;

    $data{link} =~ s{/[\w\.]+$}{/$pdf_url};

    $self->logger->info( "Storing $data{link} --> $key" );

    my $tmpf = File::Temp->new;
    $mech->get( $data{link}, ':content_file' => $tmpf->filename );

    $self->store_pdf( key      => $key,
                      datafile => $tmpf,
                      filename => $filename,
                      meta     => { description => 'Petroleum Marketing Monthly, ' . $MONTHS[ $month - 1 ] . ", $year",
                                    identifier  => $ident
                                  }
                    );
}

1;

